<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>5秒之后跳转</title>
	</head>
	<body>
		<button>跳转</button>
		<div></div>
	</body>
	<script type="text/javascript">
		//设置点击跳转
		var btn = document.querySelector('button');
		btn.addEventListener('click',function() {
			location.href = 'http://www.baidu.com';
		})
		
		//5秒钟之后跳转
		var div = document.querySelector('div');
		var time = 5;
		var timer = null;
		div.innerHTML = '还有' + time + '秒跳转';
		//设置定时器
		timer = setInterval(function() {
			//判断时间是否到期
			if(time == 0) {
				//清除定时器,跳转
				clearInterval(timer);
				location.href = 'http://www.baidu.com'
			} else {
				//时间还没到
				div.innerHTML = '还有' + time + '秒跳转';
				time--;
			}
		},1000)
	</script>
</html>
